package com.chaipuyu.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.chaipuyu.entity.*;
import com.chaipuyu.service.GoodsService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Select;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 作者: cc又瘦啦
 * 开发日期: 2021/12/20
 **/
@Controller
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @RequestMapping("toLogin")
    public String toLogin(){

        return "login";
    }

    @RequestMapping("login")
    public String login(HttpServletRequest request, User user ){

        User user1 = goodsService.selectUser(user);

        //登录成功后，将id、用户名等存储到Session（4分）
        request.getSession().setAttribute("user",user1);

        return "redirect:list";
    }

    @RequestMapping("list")
    public String list(HttpServletRequest request, Pojo pojo,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "2") Integer pageSize){

        System.err.println(pojo);

        User user = (User) request.getSession().getAttribute("user");

        PageInfo<Goods> pageInfo = goodsService.selectGoods(pojo,pageNum,pageSize,user);

        request.setAttribute("page",pageInfo);
        request.setAttribute("list",pageInfo.getList());
        request.setAttribute("pojo",pojo);

        return "list";
    }

    @RequestMapping("getBrandList")
    @ResponseBody
    public List<Brand> getBrandList(){

        List<Brand> blist = goodsService.getBrandList();

        return blist;
    }

    @RequestMapping("getTypeList")
    @ResponseBody
    public List<Type> getTypeList(){

        List<Type> list = goodsService.getTypeList();

        return list;
    }

    @RequestMapping("toAdd")
    public String toAdd(){

        return "add";
    }

    @RequestMapping("add")
    public String add(Goods goods){

        System.err.println(goods);

        int add = goodsService.insert(goods);

        return "redirect:list";
    }

    @RequestMapping("upPic")
    @ResponseBody
    public String upPic(@RequestParam("file")MultipartFile file){

        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filename = UUID.randomUUID().toString()+suffix;
        try {
            file.transferTo(new File("d:/pic/"+filename));
            return filename;

        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }
    }

    @RequestMapping("selectByusername")
    @ResponseBody
    public User selectByusername(String uname){

        User user = goodsService.selectByusername(uname);

        if (user==null){
            return null;
        }else {
            return user;
        }
    }

    @RequestMapping("selectByupwd")
    @ResponseBody
    public User selectByupwd(String uname, String upwd){

        User user = goodsService.selectByupwd(uname,upwd);

        if (user==null){
            return null;
        }else {
            return user;
        }



    }


}
